#include "NewCollectChannelDialog.h"
#include "ui_NewCollectChannelDialog.h"
#include <QHBoxLayout>
#include <QMessageBox>
NewCollectChannelDialog::NewCollectChannelDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NewCollectChannelDialog)
{
    ui->setupUi(this);
    setWindowTitle("新建通道");
    InitWidget();

}

NewCollectChannelDialog::~NewCollectChannelDialog()
{
    delete ui;
}

void NewCollectChannelDialog::SetInitChannelName(QString name)
{
    ui->lineEdit->setText(name);
    ui->lineEdit_2->setText("采集通道");
}

void NewCollectChannelDialog::InitWidget()
{

    m_widgetReserve = new QWidget();
    QHBoxLayout * hLayout4 = new QHBoxLayout();
    {
        QLabel * label1 = new QLabel("备用参数");
        QLineEdit * edit1 = new QLineEdit();
        hLayout4->addWidget(label1);
        hLayout4->addWidget(edit1);
        m_mapChilds.insert("reserve",edit1);
    }
    m_widgetReserve->setLayout(hLayout4);
    ui->stackedWidget->addWidget(m_widgetReserve);

    m_widgetSerialport = new QWidget();
    QHBoxLayout * hLayout2 = new QHBoxLayout();
    {
        QLabel * label1 = new QLabel("串口号");
        QLineEdit * edit1 = new QLineEdit();
        m_mapChilds.insert("comName",edit1);
        QLabel * label2 = new QLabel("波特率");
        QComboBox * combox = new QComboBox();
        combox->addItems(QStringList{"4800","9600","14400","19200","115200"});
        combox->setCurrentIndex(1);
         m_mapChilds.insert("baudRate",combox);
        QLabel * label3 = new QLabel("数据位");
        QComboBox * combox1 = new QComboBox();
        combox1->addItems(QStringList{"4","5","6","7","8"});
        combox1->setCurrentIndex(3);
        m_mapChilds.insert("dataBit",combox1);
        QLabel * label4 = new QLabel("检验位");
        QComboBox * combox2 = new QComboBox();
        combox2->addItems(QStringList{"奇","偶","无"});
         m_mapChilds.insert("checkBit",combox2);
        QLabel * label5 = new QLabel("停止位");
        QComboBox * combox3 = new QComboBox();
        combox3->addItems(QStringList{"1","1.5","2"});
         m_mapChilds.insert("stopBit",combox3);
        hLayout2->addWidget(label1);
        hLayout2->addWidget(edit1);
        hLayout2->addWidget(label2);
        hLayout2->addWidget(combox);
        hLayout2->addWidget(label3);
        hLayout2->addWidget(combox1 );
        hLayout2->addWidget(label4 );
        hLayout2->addWidget(combox2 );
        hLayout2->addWidget(label5 );
        hLayout2->addWidget(combox3 );
    }
    m_widgetSerialport->setLayout(hLayout2);
    ui->stackedWidget->addWidget(m_widgetSerialport);
}

void NewCollectChannelDialog::on_comboBox_currentTextChanged(const QString &arg1)
{
    if(arg1.compare("串口")==0)
    {
        ui->stackedWidget->setCurrentIndex(1);
    }else if (arg1.compare("虚拟")==0)
    {
        ui->stackedWidget->setCurrentIndex(0);
    }
}

void NewCollectChannelDialog::on_proto_Button_clicked()
{
    ProtocolDialog protocalDialog;
    protocalDialog.SetProtocalType(ProtocolDialog::Protocal_Type::Collect_Type);
    if(protocalDialog.exec()==QDialog::Accepted)
    {
        ui->lineEdit_3->setText(protocalDialog.GetSelectProtoName());
        ui->lineEdit_4->setText(protocalDialog.GetSelectProtoCate());
    }
}

stCollectChannel NewCollectChannelDialog::getChannel()
{
    m_channel.name = ui->lineEdit->text().toStdString();
    m_channel.desc = ui->lineEdit_2->text().toStdString();
    m_channel.protocate = ui->lineEdit_3->text().toStdString();
    m_channel.protoName = ui->lineEdit_4->text().toStdString();
    m_channel.portType =(stCollectChannel::ChannelPortType) ui->comboBox->currentIndex();
    m_channel.reserve = ((QLineEdit *)m_mapChilds["reserve"])->text().toStdString();
    m_channel.serialport.name = ((QLineEdit *)m_mapChilds["comName"])->text().toStdString();
    m_channel.serialport.checkBit = QString::number(((QComboBox *)m_mapChilds["checkBit"])->currentIndex()).toStdString();
    m_channel.serialport.baudRate = ((QComboBox *)m_mapChilds["baudRate"])->currentText().toStdString();
    m_channel.serialport.dataBit = ((QComboBox *)m_mapChilds["dataBit"])->currentText().toStdString();
    m_channel.serialport.stopBit = QString::number(((QComboBox *)m_mapChilds["stopBit"])->currentIndex()).toStdString();
    return m_channel;
}


void NewCollectChannelDialog::on_pushButton_clicked()
{
    if(ui->lineEdit->text().trimmed().isEmpty() || ui->lineEdit_3->text().trimmed().isEmpty())
    {
        QMessageBox::information(nullptr,"新建通道","数据输入不完整，请重新输入");
        if(ui->lineEdit->text().trimmed().isEmpty())
            ui->lineEdit->setFocus();
        else
            ui->lineEdit_3->setFocus();;
    }else
    {
        accept();
    }
}


void NewCollectChannelDialog::on_pushButton_2_clicked()
{
    close();
}

